package com.example.reone.mydatapicker;

import android.app.DialogFragment;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by wangxingsheng on 2016/10/17.
 */

public class DataPickerDialog extends DialogFragment implements View.OnClickListener, DataPickerItemView.OnDataPickerItemChangeListener {
    public final String F24H = "24H";
    public final String F12H = "12H";
    private View _sureBtn,_cancelBtn;
    private DataPickerItemView _yearItem,_monthItem,_dayItem,_hourItem,_minItem,_formatItem;
    private OnDataPickerListener _OnDataPickerListener;
    private Calendar _currentCalendar;
    private String _currentFormat = F12H;


    private int _preYear = -1,_preMonth = -1,_preDay = -1,_preHour = -1,_preMin = -1;
    private String _preFormat = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_data_picker_dialog, container);
        findContainView(view);
        initDate();
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return view;
    }

    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        //设置dialog在界面上所在宽高占比0.75 0.5
        getDialog().getWindow().setLayout((int) (dm.widthPixels * 0.75), (int) (dm.widthPixels * 0.5));
    }

    /**
     * 如果不需要预设时间格式，format传null
     * @param year
     * @param month
     * @param day
     * @param hour
     * @param min
     * @param format  F12H,F24H
     */
    public void initDate(int year,int month,int day,int hour,int min,String format){
        _preYear = year;
        _preMonth = month;
        _preDay = day;
        _preHour = hour;
        _preMin = min;
        _preFormat = format;
    }

    private void initDate(){
        _currentCalendar = Calendar.getInstance();
        if(_preYear != -1){
            _currentCalendar.set(_preYear,_preMonth-1,_preDay,_preHour,_preMin);
        }
        if(_preFormat!=null){
            _currentFormat = _preFormat;
        }else{
            //判断当前系统设定的时间格式是12H还是24H
            _currentFormat = DateFormat.is24HourFormat(getDialog().getContext())?F24H:F12H;
        }
        setCurrentDate(_currentCalendar,_currentFormat);
    }
    private void setCurrentDate(Calendar calendar,String format){
        _yearItem.setText(calendar.get(Calendar.YEAR));
        _monthItem.setText(calendar.get(Calendar.MONTH)+1);
        _dayItem.setText(calendar.get(Calendar.DAY_OF_MONTH));
        if(_currentFormat.equals(F12H)){
            int h = (calendar.get(Calendar.HOUR_OF_DAY)+12)%12;
            if(h==0){
                h = 12;
            }
            _hourItem.setText(h);
        }else{
            _hourItem.setText(calendar.get(Calendar.HOUR_OF_DAY));
        }
        _minItem.setText(calendar.get(Calendar.MINUTE));
        _formatItem.setText(format);
    }

    public void setOnDataPickerListener(OnDataPickerListener onDataPickerListener){
        _OnDataPickerListener = onDataPickerListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.data_picker_sure:
                if(_OnDataPickerListener!=null){
                    String date = "xxxx-xx-xx";
                    String time = "xx:xx";
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String currentCalendar[] = dateFormat.format(_currentCalendar.getTime()).split(" ");
                    if(currentCalendar.length>=2){
                        date = currentCalendar[0];
                        time = currentCalendar[1];
                    }
                    _OnDataPickerListener.onSure(_currentCalendar,_currentFormat,date,time);
                }
                break;
            case R.id.data_picker_cancel:
                if(_OnDataPickerListener!=null)
                    _OnDataPickerListener.onCancel();
                break;
            default:
        }
    }

    @Override
    public void onItemUpPress(View v) {
        switch (v.getId()){
            case R.id.data_picker_item_year:
                _currentCalendar.add(Calendar.YEAR,+1);
                break;
            case R.id.data_picker_item_month:
                _currentCalendar.add(Calendar.MONTH,+1);
                break;
            case R.id.data_picker_item_day:
                _currentCalendar.add(Calendar.DAY_OF_MONTH,+1);
                break;
            case R.id.data_picker_item_hour:
                _currentCalendar.add(Calendar.HOUR_OF_DAY,+1);
                break;
            case R.id.data_picker_item_min:
                _currentCalendar.add(Calendar.MINUTE,+1);
                break;
            case R.id.data_picker_item_format:
                switchFormat();
                break;
            default:
        }
        setCurrentDate(_currentCalendar,_currentFormat);
    }

    @Override
    public void onItemBottomPress(View v) {
        switch (v.getId()){
            case R.id.data_picker_item_year:
                _currentCalendar.add(Calendar.YEAR,-1);
                break;
            case R.id.data_picker_item_month:
                _currentCalendar.add(Calendar.MONTH,-1);
                break;
            case R.id.data_picker_item_day:
                _currentCalendar.add(Calendar.DAY_OF_MONTH,-1);
                break;
            case R.id.data_picker_item_hour:
                _currentCalendar.add(Calendar.HOUR_OF_DAY,-1);
                break;
            case R.id.data_picker_item_min:
                _currentCalendar.add(Calendar.MINUTE,-1);
                break;
            case R.id.data_picker_item_format:
                switchFormat();
                break;
            default:
        }
        setCurrentDate(_currentCalendar,_currentFormat);
    }

    private void switchFormat(){
        if(F12H.equals(_currentFormat)){
            _currentFormat = F24H;
        }else{
            _currentFormat = F12H;
        }
    }

    public interface OnDataPickerListener{
        void onSure(Calendar currentDate,String currentFormat,String date,String time);
        void onCancel();
    }
    private void findContainView(View view){
        _sureBtn = view.findViewById(R.id.data_picker_sure);
        _cancelBtn = view.findViewById(R.id.data_picker_cancel);
        _yearItem = (DataPickerItemView) view.findViewById(R.id.data_picker_item_year);
        _monthItem = (DataPickerItemView) view.findViewById(R.id.data_picker_item_month);
        _dayItem = (DataPickerItemView) view.findViewById(R.id.data_picker_item_day);
        _hourItem = (DataPickerItemView) view.findViewById(R.id.data_picker_item_hour);
        _minItem = (DataPickerItemView) view.findViewById(R.id.data_picker_item_min);
        _formatItem = (DataPickerItemView) view.findViewById(R.id.data_picker_item_format);
        _sureBtn.setOnClickListener(this);
        _cancelBtn.setOnClickListener(this);

        _yearItem.setOnDataPickerItemChangeListener(this);
        _monthItem.setOnDataPickerItemChangeListener(this);
        _dayItem.setOnDataPickerItemChangeListener(this);
        _hourItem.setOnDataPickerItemChangeListener(this);
        _dayItem.setOnDataPickerItemChangeListener(this);
        _hourItem.setOnDataPickerItemChangeListener(this);
        _minItem.setOnDataPickerItemChangeListener(this);
        _formatItem.setOnDataPickerItemChangeListener(this);
    }
}
